<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$fields=array('id','uniacid','title','type','status');
	$list=pdo_getslice('jy_yxw_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		$params=array('uniacid'=>$_SESSION['uniacid']);
		if($_RQ['jiao_time']){
			$time_range=cfc('tools')->base64_decode($_RQ['jiao_time'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$return['params']['jiao_time']=$time_range;
			$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
		}
		$heparams=$params;
		foreach($list as &$item){
			$params['storeid']=$item['id'];
			$heparams['hestoreid']=$item['id'];
			$params['type']=0;
			$item['fee1']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['fee1jian']=pdo_getsum('jy_yxw_feelog',$params,'hefee');
			if($item['fee1jian']){
				$item['fee1']-=$item['fee1jian'];
			}
			$heparams['type']=0;
			$item['fee1he']=pdo_getsum('jy_yxw_feelog',$heparams,'hefee');
			if($item['fee1he']){
				$item['fee1']+=$item['fee1he'];
			}
			$item['bufee']=pdo_getsum('jy_yxw_feelog',$params,'bufee');
			$item['jiaobenfee']=pdo_getsum('jy_yxw_feelog',$params,'jiaobenfee');
			$item['zufee']=pdo_getsum('jy_yxw_feelog',$params,'zufee');
			$item['peifee']=pdo_getsum('jy_yxw_feelog',$params,'peifee');
			$item['zhaofee']=pdo_getsum('jy_yxw_feelog',$params,'zhaofee');
			$params['type']=1;
			$item['fee2']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['fee2jian']=pdo_getsum('jy_yxw_feelog',$params,'hefee');
			if($item['fee2jian']){
				$item['fee2']-=$item['fee2jian'];
			}
			$heparams['type']=1;
			$item['fee2he']=pdo_getsum('jy_yxw_feelog',$heparams,'hefee');
			if($item['fee2he']){
				$item['fee2']+=$item['fee2he'];
			}
			$params['type']=2;
			$item['fee3']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['fee3jian']=pdo_getsum('jy_yxw_feelog',$params,'hefee');
			if($item['fee3jian']){
				$item['fee3']-=$item['fee3jian'];
			}
			$heparams['type']=2;
			$item['fee3he']=pdo_getsum('jy_yxw_feelog',$heparams,'hefee');
			if($item['fee3he']){
				$item['fee3']+=$item['fee3he'];
			}
			$params['type']=3;
			if($item['type']==0){
				$item['fee4']=pdo_getsum('jy_yxw_feelog',$params,'zhaofee');
			}elseif($item['type']==1){
				$fee4=pdo_getsum('jy_yxw_feelog',$params,'fee');
				unset($params['storeid']);
				$params['jbstoreid']=$item['id'];
				$item['fee4']=pdo_getsum('jy_yxw_feelog',$params,'jiaobenfee');
				$item['fee4'] +=$fee4;
				unset($params['jbstoreid']);
			}elseif($item['type']==2){
				//总部
				unset($params['storeid']);
				$item['fee4']=pdo_getsum('jy_yxw_feelog',$params,'zufee');
			}
			$params['type']=4;
			$params['storeid']=$item['id'];
			$item['fee5']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['allfee']=price($item['fee1']+$item['fee2']+$item['fee3']+$item['bufee']-$item['fee4']-$item['fee5'],'int');
			$item['now']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'status'=>1,'storeid'=>$item['id'],'jiao_time >'=>$nowyue),'fee');
			$item['yes']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'status'=>1,'storeid'=>$item['id'],'jiao_time between'=>array($yesyue,$nowyue)),'fee');
		}
	}
	paixu($list,'allfee','desc');
	foreach($list as $k=>&$lv){
		$lv['rank']=$k+1;
	}
	$heji=array('title'=>'合计');
	foreach($list as $v){
		$heji['fee2'] +=$v['fee2'];
		$heji['fee1'] +=$v['fee1'];
		$heji['bufee'] +=$v['bufee'];
		$heji['fee5'] +=$v['fee5'];
		$heji['fee4'] +=$v['fee4'];
		$heji['fee3'] +=$v['fee3'];
		$heji['allfee'] +=$v['allfee'];
		$heji['jiaobenfee'] +=$v['jiaobenfee'];
		$heji['zufee'] +=$v['zufee'];
		$heji['peifee'] +=$v['peifee'];
		$heji['now'] +=$v['now'];
		$heji['yes'] +=$v['yes'];
	}
	$heji['allfee']=price($heji['allfee'],'int');
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',		
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排名','type'=>'string'),
			array('name'=>'title','title'=>'门店','type'=>'string'),
			array('name'=>'fee2','title'=>'定金','type'=>'string'),
			array('name'=>'fee1','title'=>'单费','type'=>'string'),
			array('name'=>'fee3','title'=>'分期缴费','type'=>'string'),
			array('name'=>'bufee','title'=>'著作权','type'=>'string'),
			array('name'=>'fee5','title'=>'退定金','type'=>'string'),
			array('name'=>'fee4','title'=>'退单费','type'=>'string'),
			array('name'=>'allfee','title'=>'实收合计','type'=>'string'),
			array('name'=>'jiaobenfee','title'=>'售后成本','type'=>'string'),
			//array('name'=>'zufee','title'=>'著作权','type'=>'string'),
			//array('name'=>'peifee','title'=>'培训费','type'=>'string'),
			//array('name'=>'zhaofee','title'=>'招生费','type'=>'string'),
			array('name'=>'now','title'=>'当月业绩','type'=>'string'),
			array('name'=>'yes','title'=>'上月业绩','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'jiao_time','width'=>10,'title'=>'交费日期','type'=>'time_range'),
		)
	);
	$return['store']=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
